package top.wilsonlv.jaguar.cloud.auth.filter;

import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
import top.wilsonlv.jaguar.cloud.auth.config.AuthServerWebSecurityConfigurer;
import top.wilsonlv.jaguar.cloud.handlerlog.client.component.HandlerLogInterceptor;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lvws
 * @since 2022/8/15
 */
@Component
public class LogoutLogFilter extends OncePerRequestFilter {

    @Autowired(required = false)
    private HandlerLogInterceptor handlerLogInterceptor;

    @Override
    protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response,
                                    @NonNull FilterChain filterChain) throws ServletException, IOException {
        if (handlerLogInterceptor != null && AuthServerWebSecurityConfigurer.LOGOUT_PATH.equals(request.getRequestURI())) {
            handlerLogInterceptor.log(request, response, null, null);
        }
        filterChain.doFilter(request, response);
    }
}
